package com.servlet.login.root.servlet;

import com.servlet.login.root.bean.User;
import com.servlet.login.root.service.UserService;
import com.servlet.login.root.service.impl.UserServiceImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;



public class UserLoginPwdTipsServlet extends HttpServlet {
    private UserService  userService =  new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override  //这是对用户密码检测后台代码
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String password = req.getParameter("password");
        String username = req.getParameter("username");
        Boolean matchePwd = userService.isMatchePwd(username,password);
        User user = new User();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter printWriter = resp.getWriter();
        JSONObject jsonObject = null;
        if (password.isEmpty() || !matchePwd) {
            user.setPassword("×");
        }else {
            user = userService.returnUser(username);
            user.setPassword("√");
        }
        System.out.println(user.toString());
        jsonObject = JSONObject.fromObject(user);
        System.out.println(jsonObject.toString());
        printWriter.print(jsonObject);
        printWriter.flush();
    }
}
